package chapterSystem.client.interfaceDrawing.activities;

import chapterSystem.client.model.entitiesInt.Brother;
import chapterSystem.client.model.entitiesInt.Report;
import chapterSystem.client.model.enums.ActivityType;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.*;

import java.util.List;

/**
 * User: Hasp
 * Date: 06.05.12
 * Time: 17:50
 */
public class SeeReportsActivity extends Activity {
    private final String title;
    private final List<Report> reports;
    private final Activity callback;

    public SeeReportsActivity(final Brother brother, final ActivityType type, String title, List<Report> reports,
                                 Activity callback) {
        super(brother, type);
        this.title = title;
        this.reports = reports;
        this.callback = callback;
    }

    @Override
    public void drawActivity(final StackLayoutPanel workArea) {
        workArea.clear();

        final VerticalPanel mainPanel = new VerticalPanel();
        mainPanel.setWidth("100%");
        mainPanel.setHeight("100%");
        mainPanel.setBorderWidth(3);

        for (final Report report : reports) {
            final VerticalPanel midPanel = new VerticalPanel();
            midPanel.setWidth("100%");
            midPanel.setHeight("100%");

            midPanel.add(new HTML("<b><i>Date: " + report.getDate().toString() + "</i></b><p>"));
            midPanel.add(new HTML(report.getReport()));

            if (report.getAuthorId() == brother.getId()) {
                final Button editButton = new Button("Edit Report");
                editButton.addClickHandler(new ClickHandler() {
                    @Override
                    public void onClick(ClickEvent event) {
                        new EditReportActivity(brother, ActivityType.REPORT, 0, SeeReportsActivity.this, report).drawActivity(workArea);
                    }
                });

                midPanel.add(editButton);
                midPanel.setCellHeight(editButton, "25px");
                midPanel.setCellHorizontalAlignment(editButton, HasAlignment.ALIGN_CENTER);
            }
            mainPanel.add(midPanel);
        }

        final Button back = new Button("<< Back");
        back.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                callback.drawActivity(workArea);
            }
        });

        mainPanel.add(back);
        workArea.add(new ScrollPanel(mainPanel), new HTML(title), 4);
    }
}
